package com.tmy.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tmy.common.Constants;
import com.tmy.common.enums.ResultCodeEnum;
import com.tmy.common.enums.RoleEnum;
import com.tmy.entity.Account;
import com.tmy.entity.Manager;
import com.tmy.exception.CustomException;
import com.tmy.mapper.ManagerMapper;
import com.tmy.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员业务处理
 **/
@Service
public class ManagerServiceImpl {

    @Resource
    private ManagerMapper managerMapper;

    /**
     * 新增
     */
    public void add(Manager manager) {
        Manager dbManager = managerMapper.selectByUsername(manager.getUsername());
        if (ObjectUtil.isNotNull(dbManager)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(manager.getPassword())) {
            manager.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(manager.getName())) {
            manager.setName(manager.getUsername());
        }
        manager.setRole(RoleEnum.ADMIN.name());
        managerMapper.insert(manager);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        managerMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            managerMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Manager manager) {
        managerMapper.updateById(manager);
    }

    /**
     * 根据ID查询
     */
    public Manager selectById(Integer id) {
        return managerMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Manager> selectAll(Manager manager) {
        return managerMapper.selectAll(manager);
    }

    /**
     * 分页查询
     */
    public PageInfo<Manager> selectPage(Manager manager, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Manager> list = managerMapper.selectAll(manager);
        return PageInfo.of(list);
    }

    /**
     * 登录
     */
    public Account login(Account account) {
        Account dbManager = managerMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbManager)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbManager.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token
        String tokenData = dbManager.getId() + "-" + RoleEnum.MANAGER.name();
        String token = TokenUtils.createToken(tokenData, dbManager.getPassword());
        dbManager.setToken(token);
        return dbManager;
    }

    /**
     * 注册
     */
    public void register(Account account) {
        Manager manager = new Manager();
        BeanUtils.copyProperties(account, manager);
        add(manager);
    }

    /**
     * 修改密码
     */
    public void updatePassword(Account account) {
        Manager dbManager = managerMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbManager)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbManager.getPassword())) {
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
        dbManager.setPassword(account.getNewPassword());
        managerMapper.updateById(dbManager);
    }

}